package com.kunya.product.service.impl.active;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.active.ProductSeckillMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import java.util.Date;

/**
 * @author zp
 * @Description: 商品秒杀service接口实现类
 * @date 2018/7/19 16:08
 */
public class ProdutSeckillServiceImpl {
    //此接口所用混淆
    private String salt="1z24v23[0~!@#$%&^#ca)(*$#@@a42";

    private ProductSeckillMapper productSeckillMapper;

    private String getMd5(Long seckillId){
        String base=seckillId+"/"+salt;
        String md5= DigestUtils.md5DigestAsHex(base.getBytes());
        return md5;
    }
    /**
     * @author zp
     * @Description: 执行秒杀商品方法
     * @param
     * @date 2018/7/19 17:58
     */
    @Transactional
    public ServerResponse seckill(Long seckillId,String md5){
        if (seckillId==null|| StringUtils.isEmpty(md5))
        {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        //减库存
        Date now=new Date();
         int resultCount=productSeckillMapper.updateStock(seckillId,now);
         if(resultCount<=0){
             return ServerResponse.createByErrorMessage("秒杀失败");
         }
         int result=0;//todo 插入订单
         if(result<=0){
             return ServerResponse.createByErrorMessage("重复异常");
         }
         //执行生成订单操作
        return ServerResponse.createBySuccess();
    }
}
